<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/5/19
 * Time: 15:45
 */

namespace app\admin\controller;


use app\common\model\Dictionary as DictionaryModel;
use think\Exception;

class Dictionary extends Admin
{

    /**
     * 三方油站价格配置信息列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList(){
        $page = input('page',1);
        $limit = input('limit',20);
        $keyWords = input('search_word','','trim');
        $type = input('type','');
        $param = [];
        if($keyWords){
            $param['name|common'] = ['like','%'.$keyWords.'%'];
        }
        if($type !== ''){
            $param['dictionary_type'] = $type;
        }
        $model = new DictionaryModel();
        $count = $model->where($param)->count();
        if($page > ceil($count/$limit)){
            $page = ceil($count/$limit);
        }
        $list = $model->where($param)->page($page, $limit)->order('id desc')->select();

        return json_tab_msg($count, $page, $list);
    }


    public function dictionaryModify()
    {
        $data['id']= input('id');
        $data['name'] = input('name','','trim');
        $data['dictionary_type'] = input('dictionary_type');
        $data['sort'] = input('sort');
        $data['common'] = input('common','','trim');

        if($data['dictionary_type'] == 1){
            $data['type_name'] = '支付方式';
        }
        $daytime = date('Y-m-d H:i:s');
        $model =new DictionaryModel();
        try {
            if($data['id']){ //修改用户信息
                $result = $this->validate($data,'Dictionary.update');
                if ($result !== true) json_return_code('',$result,201);
                $data['update_time'] = $daytime;
                $model->save($data,array('id'=>$data['id']));
            }else{ //新增用户信息
                $result = $this->validate($data,'Dictionary.add');
                if ($result !== true) json_return_code('',$result,201);
                $data['create_time'] = $daytime;
                $model->insert($data);
            }
            json_return_code();
        }catch (Exception $e){
            json_return_code('','操作失败'.$e->getMessage(),201);
        }
    }

    /**
     * 后台管理用户删除
     * @return \think\response\Json
     */
    public function del()
    {
        $id = input('id');
        if(!$id){
            json_return_code('','请选择数据',201);
        }
        $model =new DictionaryModel();
        try {
            $model::destroy($id);
            json_return_code( );
        }catch (\Exception $e){
            json_return_code('','删除失败，请重试!'.$e->getMessage(),201);
        }
    }

    /**
     * 获取字典信息
     * @return void
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function getListByType()
    {
        $type = input('type');
        $param = [];
        if($type){
            $param['dictionary_type'] = ['in',$type];
        }
        $model = new DictionaryModel();
        $list = $model->where($param)->order('sort asc')->select();
        $arr = [
            'pay_type'=>[],
        ];
        foreach ($list as $item){
            if($item['dictionary_type'] == 1){
                $arr['pay_type'][] = $item;
            }
        }
        json_return_code($arr);
    }
}